package com.symaster.common.swing.element.polygon;

import com.symaster.common.geom.entity.Polygon;

import java.awt.*;
import java.util.List;
import java.util.*;

public class JPolygon extends com.symaster.common.geom.entity.Polygon implements List<JPolygon> {
    private final List<JPolygon> jPolygons;
    private Color polygonColor;

    public static JPolygon ofPolygon(Polygon polygon) {
        return new JPolygon(polygon.getXpoints(), polygon.getYpoints());
    }

    @Override
    public double getX() {
        double x = super.getX();
        double minX = jPolygons.stream().mapToDouble(JPolygon::getX).min().orElse(x);
        return Math.min(x, minX);
    }

    @Override
    public double getY() {
        double y = super.getY();
        double minY = jPolygons.stream().mapToDouble(JPolygon::getY).min().orElse(y);
        return Math.min(y, minY);
    }

    @Override
    public double getMaxX() {
        double maxX = super.getMaxX();
        double x = jPolygons.stream().mapToDouble(JPolygon::getMaxX).max().orElse(maxX);
        return Math.max(maxX, x);
    }

    @Override
    public double getMaxY() {
        double maxY = super.getMaxY();
        double y = jPolygons.stream().mapToDouble(JPolygon::getMaxY).max().orElse(maxY);
        return Math.max(maxY, y);
    }

    @Override
    public void translate(double xn, double yn) {
        super.translate(xn, yn);
        jPolygons.forEach(e -> e.translate(xn, yn));
        update();
    }

    public JPolygon(double[] xPoints, double[] yPoints) {
        super(xPoints, yPoints);
        this.jPolygons = new ArrayList<>();
        this.polygonColor = new Color(10, 10, 10);
    }

    public JPolygon(double[] xPoints, double[] yPoints, Color polygonColor) {
        super(xPoints, yPoints);
        this.jPolygons = new ArrayList<>();
        this.polygonColor = polygonColor;
    }

    public void setPolygonColor(Color polygonColor) {
        this.polygonColor = polygonColor;
    }

    public Color getPolygonColor() {
        return polygonColor;
    }

    @Override
    public int size() {
        return jPolygons.size();
    }

    @Override
    public boolean contains(Object o) {
        return jPolygons.contains(o);
    }

    @Override
    public Iterator<JPolygon> iterator() {
        return jPolygons.iterator();
    }

    @Override
    public Object[] toArray() {
        return jPolygons.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return jPolygons.toArray(a);
    }

    @Override
    public boolean add(JPolygon jPolygon) {
        jPolygons.add(jPolygon);
        update();
        return true;
    }

    @Override
    public boolean remove(Object o) {
        return jPolygons.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return new HashSet<>(jPolygons).containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends JPolygon> c) {
        jPolygons.addAll(c);
        update();
        return true;
    }

    @Override
    public boolean addAll(int index, Collection<? extends JPolygon> c) {
        jPolygons.addAll(index, c);
        return true;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return jPolygons.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return jPolygons.retainAll(c);
    }

    @Override
    public void clear() {
        jPolygons.clear();
    }

    @Override
    public JPolygon get(int index) {
        return jPolygons.get(index);
    }

    @Override
    public JPolygon set(int index, JPolygon element) {
        return jPolygons.set(index, element);
    }

    @Override
    public void add(int index, JPolygon element) {
        jPolygons.add(index, element);
        update();
    }

    @Override
    public JPolygon remove(int index) {
        return jPolygons.remove(index);
    }

    @Override
    public int indexOf(Object o) {
        return jPolygons.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return jPolygons.lastIndexOf(o);
    }

    @Override
    public ListIterator<JPolygon> listIterator() {
        return jPolygons.listIterator();
    }

    @Override
    public ListIterator<JPolygon> listIterator(int index) {
        return jPolygons.listIterator(index);
    }

    @Override
    public List<JPolygon> subList(int fromIndex, int toIndex) {
        return jPolygons.subList(fromIndex, toIndex);
    }
}